/**
 * 公司：金丹科技
 * 文件：LoginHanderinterceptor
 * 作者：kong
 * 时间：2019/4/16 14:28
 * 描述：
 */

package com.springboot.test.component;


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginHanderinterceptor implements HandlerInterceptor {

//   目标方法执行之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("loginuser");
         if(user == null){
//            未被登录，返回登录
             request.setAttribute("msg","没有权限直接访问");
             request.getRequestDispatcher("/index.html").forward(request,response);
             return false;
         }else{
//             已经登录 放行
             return true;
         }

    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
